package com.sudworth.repository.module;


import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.sudworth.repository.BuildConfig;
import com.sudworth.repository.RepositorySDK;
import com.sudworth.repository.db.DBProxy;
import com.sudworth.repository.network.ApiServices;
import com.sudworth.repository.network.okhttp.OkHttpEngine;
import com.sudworth.repository.repository.IRepositoryManager;
import com.sudworth.repository.repository.RepositoryManager;
import com.sudworth.repository.repository.data.CommDataSource;
import com.sudworth.repository.repository.data.MsgDataSource;
import com.sudworth.repository.repository.data.OrderDataSource;
import com.sudworth.repository.repository.data.PeqDataSource;
import com.sudworth.repository.repository.data.UserDataSource;
import com.sudworth.repository.repository.data.source.CommRepository;
import com.sudworth.repository.repository.data.source.MsgRepository;
import com.sudworth.repository.repository.data.source.OrderRepository;
import com.sudworth.repository.repository.data.source.PeqRepository;
import com.sudworth.repository.repository.data.source.UserRepository;

import javax.inject.Singleton;

import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;

/**
 * 数据仓库相关
 */
@Module
@InstallIn(SingletonComponent.class)
public abstract class RepositoryModule {

    @Provides
    static Context provideContext(Application application) {
        return application.getApplicationContext();
    }

    @Provides
    static SharedPreferences provideSharePreferences(Application context) {
        return context.getSharedPreferences(RepositorySDK.sp_name, Context.MODE_PRIVATE);
    }

    @Singleton
    @Binds
    abstract IRepositoryManager bindRepositoryManager(RepositoryManager manager);

    @Binds
    abstract CommDataSource bindCommDataSource(CommRepository repository);

    @Binds
    abstract PeqDataSource bindPeqDataSource(PeqRepository repository);

    @Binds
    abstract OrderDataSource bindOrderDataSource(OrderRepository repository);

    @Binds
    abstract MsgDataSource bindMsgDataSource(MsgRepository repository);

    @Binds
    abstract UserDataSource bindUserDataSource(UserRepository repository);

    @Singleton
    @Provides
    static ApiServices provideApiServices(Application context, SharedPreferences sp) {
        return OkHttpEngine.get(sp).create(ApiServices.class, context, BuildConfig.BASE_URL);
    }

    @Singleton
    @Provides
    static DBProxy provideAppDatabase() {
        return DBProxy.getInstance();
    }
}
